package com.wynn.controller;

import com.wynn.pojo.EquipError;
import com.wynn.pojo.Equipment;
import com.wynn.pojo.Result;
import com.wynn.serviceFeign.EquipmentClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("equip")
public class EquipController {

    @Autowired
    private EquipmentClient equipmentClient;

    //首页 查询设备概况
    @GetMapping("selectCondition")
    public Result selectCondition(){
        Result result = equipmentClient.selectEquipCondition();
        return result;
    }

    //查询设备表
    @GetMapping("selectAll")
    public Result selectAll(){
        return equipmentClient.selectAll();
    }

    //添加设备表
    @PreAuthorize("hasRole('equipment') || hasRole('admin')")
    @PostMapping("add")
    public Result add(@RequestBody Equipment equipment){

        return equipmentClient.add(equipment);
    }

    //修改设备表
    @PostMapping("edit")
    @PreAuthorize("hasRole('equipment') || hasRole('admin')")
    public Result edit(@RequestBody Equipment equipment){
        return equipmentClient.edit(equipment);
    }

    //删除设备状况
    @GetMapping("delete")
    @PreAuthorize("hasRole('equipment') || hasRole('admin')")
    public Result delete(int id){
        return equipmentClient.delete(id);
    }

    //获取设备名称列表
    @GetMapping("getEquipName")
    public Result getEquipName(){
        return equipmentClient.getEquipName();
    }

    //条件搜索
    @GetMapping("search")
    public Result search(String name,String line,String equipNum){
        return equipmentClient.search(name,line,equipNum);
    }

    //搜索故障设备记录
    @GetMapping("selectEquipError")
    public Result selectEquipError(){
        return equipmentClient.selectEquipError();
    }

    //搜索非故障的设备
    @GetMapping("selectEquipAva")
    public Result selectEquipAvar(){
        Result result = new Result();
        try {
            result = equipmentClient.selectEquipAva();
            result.setCode("200");
            result.setMessage("查询设备成功");
        }catch (Exception e){
            log.warn("查询非故障设备失败");
            result.setCode("503");
            result.setMessage("查询设备失败");
        }
        return result;
    }

    //增加故障设备记录
    @PostMapping("addEquipError")
    @PreAuthorize("hasRole('equipment') || hasRole('admin')")
    public Result addEquipError(@RequestBody EquipError equipError){
        Result result = new Result();
        try {
            equipmentClient.addEquipError(equipError);
            result.setCode("200");
            result.setMessage("添加成功");
        }catch (Exception e){
            log.warn("添加设备故障记录失败");
            result.setCode("503");
            result.setMessage("添加失败");
        }
        return result;
    }

    //修改故障设备记录
    @PostMapping("editEquipError")
    @PreAuthorize("hasRole('equipment') || hasRole('admin')")
    public Result editEquipError(@RequestBody Map<String,Object> editMap){
        Result result = new Result();
        try{
            equipmentClient.editEquipError(editMap);
            result.setCode("200");
            result.setMessage("修改成功");
        }catch (Exception e){
            log.warn("修改设备故障记录失败");
            result.setCode("503");
            result.setMessage("修改失败");
        }
        return result;
    }

    //删除故障设备记录
    @GetMapping("delEquipError")
    @PreAuthorize("hasRole('equipment') || hasRole('admin')")
    public Result delEquipError(int id){
        Result result = new Result();
        try {
            equipmentClient.delEquipError(id);
            result.setCode("200");
            result.setMessage("记录删除成功");
        }catch (Exception e){
            log.warn("删除设备故障记录失败");
            result.setCode("503");
            result.setMessage("记录删除失败");
        }
        return result;
    }

    //派修故障设备
    @PostMapping("fixEquipError")
    @PreAuthorize("hasRole('equipment') || hasRole('admin')")
    public Result fixEquipError(@RequestBody EquipError equipError){
        Result result = new Result();
        try{
            equipmentClient.fixEquipError(equipError);
            result.setCode("200");
            result.setMessage("故障设备派修成功");
        }catch (Exception e){
            log.warn("派修设备故障记录失败");
            result.setCode("503");
            result.setMessage("故障设备派修失败");
        }
        return result;
    }

    //审核维修完成的设备
    @PostMapping("checkEquipError")
    @PreAuthorize("hasRole('equipment') || hasRole('admin')")
    public Result checkEquipError(@RequestBody EquipError equipError){
        Result result = new Result();
        try{
            equipmentClient.checkEquipError(equipError);
            result.setCode("200");
            result.setMessage("故障设备审核完成");
        }catch (Exception e){
            log.warn("审核设备故障记录失败");
            result.setCode("503");
            result.setMessage("故障设备审核失败");
        }
        return result;
    }

    //按条件查询故障设备记录
    @GetMapping("equipErrorSearch")
    @PreAuthorize("hasRole('equipment') || hasRole('admin')")
    public Result equipErrorSearch(String equipNum,String productLine,String status){
        Result result = new Result();
        try{
            result = equipmentClient.equipErrorSearch(equipNum,productLine,status);
            result.setCode("200");
            result.setMessage("查询成功");
        }catch (Exception e){
            log.warn("搜索设备故障记录失败");
            result.setCode("503");
            result.setMessage("查询失败");
        }
        return result;
    }

}
